package com.patMall.service.impl;

import com.patMall.controller.AddressController;
import com.patMall.entity.*;
import com.patMall.mapper.*;
import com.patMall.service.CommodityService;
import com.patMall.service.OrderInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 750909637@qq.com
 * @PackageName patMall
 * @Package com.patMall.service.impl
 * @Date 2024/1/23 17:51
 * @Version 1.0
 */
@Service
public class OrderInfoServiceImpl implements OrderInfoService {
    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Resource
    private CommodityMapper commodityMapper;
    @Resource
    private StoreMapper storeMapper;
    @Resource
    private CommodityImgMapper commodityImgMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserAddressMapper userAddressMapper;
    @Override
    public int addOrder(OrderInfo order) {
        return orderInfoMapper.insertOrder(order);
    }

    @Override
    public int updateOrder(OrderInfo order) {
        return orderInfoMapper.updateOrder(order);
    }

    @Override
    public List<OrderInfo> getOrderUser(Integer userId) {
        List<OrderInfo> orderInfoList = orderInfoMapper.selectOrderUser(userId);
        for (OrderInfo orderInfo : orderInfoList){
            Commodity commodity = commodityMapper.getCommodityId(orderInfo.getCommodityId());
            List<CommodityImg> commodityImg = commodityImgMapper.getAllByCommodity_id(commodity.getCommodityId());
            commodity.setCommodityImgList(commodityImg);
            Store store = storeMapper.selectByStoreId(commodity.getCommodityStoreId());
            commodity.setStore(store);
            orderInfo.setCommodity(commodity);
        }
        return orderInfoList;
    }

    @Override
    public List<OrderInfo> getOrderInfo() {
        List<OrderInfo> orderInfoList = orderInfoMapper.selectOrderInfo();
        for (OrderInfo orderInfo : orderInfoList){
            Commodity commodity = commodityMapper.getCommodityId(orderInfo.getCommodityId());
            List<CommodityImg> commodityImg = commodityImgMapper.getAllByCommodity_id(commodity.getCommodityId());
            commodity.setCommodityImgList(commodityImg);
            orderInfo.setCommodity(commodity);
            User user = userMapper.selectByPrimaryKey(orderInfo.getUserId());
            UserAddress address = userAddressMapper.selectByPrimaryKey(orderInfo.getAddressId());
            orderInfo.setAddress(address);
            orderInfo.setUser(user);
        }
        return orderInfoList;
    }

    @Override
    public List<OrderInfo> findOrderInfoId(String orderId) {
        List<OrderInfo> orderInfoList = orderInfoMapper.selectOrderInfoId(orderId);
        for (OrderInfo orderInfo : orderInfoList){
            Commodity commodity = commodityMapper.getCommodityId(orderInfo.getCommodityId());
            List<CommodityImg> commodityImg = commodityImgMapper.getAllByCommodity_id(commodity.getCommodityId());
            commodity.setCommodityImgList(commodityImg);
            orderInfo.setCommodity(commodity);
            User user = userMapper.selectByPrimaryKey(orderInfo.getUserId());
            UserAddress address = userAddressMapper.selectByPrimaryKey(orderInfo.getAddressId());
            orderInfo.setAddress(address);
            orderInfo.setUser(user);
        }
        return orderInfoList;
    }
}
